package com.yuncheng.oaApi.controller;

import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.OaSysTzMessage;
import com.yuncheng.oaApi.service.IOaSysTzMessageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import com.yuncheng.system.api.SystemAPI;
import org.springframework.context.annotation.Lazy;

/**
 * @Description: 用户通知信息表
 * @Author: yuncheng
 * @Date: 2023-09-22
 * @Version: V1.0
 */
@Api(tags = "用户通知信息表接口")
@RestController
@RequestMapping("/api/spzx-oa/sysTzMessage")
public class OaSysTzMessageController extends BaseController<OaSysTzMessage, IOaSysTzMessageService> {

    private static final Logger log = LoggerFactory.getLogger(OaSysTzMessageController.class);

    @Resource
    @Lazy
    private IOaSysTzMessageService sysTzMessageService;

    @Resource
    @Lazy
    private SystemAPI systemAPI;

    /**
     * @param:
     * @Description: 查询自己的通知信息
     * @auther: lhb
     * @date: 2023/9/23 11:28
     */
    @AutoLog(value = "用户通知信息表-分页列表查询")
    @ApiOperation(value = "用户通知信息表-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(OaSysTzMessage oaSysTzMessage, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        String id = "";
        if (StringUtils.isBlank(oaSysTzMessage.getId())) {
            id = user.getId();
        }
        LambdaQueryWrapper<OaSysTzMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OaSysTzMessage::getFjsuserid, id);
        if (StringUtils.isBlank(oaSysTzMessage.getFsfyd())){
            queryWrapper.eq(OaSysTzMessage::getFsfyd, OaCommonFlowConstant.TZ_ZTWCK);
        }

        Page<OaSysTzMessage> page = new Page<>(pageNo, pageSize);
        IPage<OaSysTzMessage> pageList = sysTzMessageService.page(page, queryWrapper);
        return HttpResult.ok(pageList);
    }


    /**
     * @param:
     * @Description: 已读自己的信息通知 /批量已读
     * @auther: lhb
     * @date: 2023/9/23 11:28
     */
    @AutoLog(value = "通过id批量已读自己的信息通知 /批量已读")
    @ApiOperation(value = "通过id批量已读自己的信息通知 /批量已读")
    @GetMapping(value = "/getTzMessage")
    public HttpResult<?> getTzMessage(@RequestParam("ids") String ids) {
        List<String> msaIdList = Arrays.asList(ids.split(","));
        List<OaSysTzMessage> sjList = sysTzMessageService.listByIds(msaIdList);
        if (sjList == null) {
            return HttpResult.error("数据丢失，请联系管理员");
        }
        for (OaSysTzMessage sj : sjList) {
            sj.setFsfyd(OaCommonFlowConstant.TZ_ZTYCK);
            sysTzMessageService.updateById(sj);
        }
        return HttpResult.ok("信息状态更新成功");

    }

    /**
     * @param:
     * @Description: 已读自己的信息通知
     * @auther: lhb
     * @date: 2023/9/23 11:28
     */
    @AutoLog(value = "通过id已读自己的信息通知")
    @ApiOperation(value = "通过id已读自己的信息通知")
    @GetMapping(value = "/getMessage")
    public HttpResult<?> getMessage(@RequestParam("id") String id) {
        CurrentUserVo user = ThreadContextHelper.getCurrentUser();
        OaSysTzMessage byId = sysTzMessageService.getById(id);
        if (byId == null) {
            return HttpResult.error("数据丢失，请联系管理员");
        }
        byId.setFsfyd(OaCommonFlowConstant.TZ_ZTYCK);
        sysTzMessageService.updateById(byId);
        List<OaSysTzMessage> tzMessageList = sysTzMessageService.list(new LambdaQueryWrapper<OaSysTzMessage>()
                                                               .eq(OaSysTzMessage::getFsfyd, OaCommonFlowConstant.TZ_ZTWCK)
                                                               .eq(OaSysTzMessage::getFjsuserid, user.getId()));
        return HttpResult.ok("信息状态更新成功"+tzMessageList.size());

    }


//    /**
////     * 添加
////     *
////     * @param sysTzMessage
////     * @return
////     */
////    @AutoLog(value = "用户通知信息表-添加")
////    @ApiOperation(value = "用户通知信息表-添加")
////    //@RequiresPermissions("message:SysTzMessage:add")
////    @PostMapping(value = "/add")
////    public HttpResult<?> add(@RequestBody SysTzMessage sysTzMessage) {
////        HttpResult<String> result = new HttpResult(true);
////        sysTzMessageService.save(sysTzMessage);
////        result.setResult(sysTzMessage.getId());
////        result.setMessage(I18nUtil.message("save.success"));
////        return result;
////    }

    /**
     * 编辑
     *
     * @param oaSysTzMessage
     * @return
     */
    @AutoLog(value = "用户通知信息表-编辑")
    @ApiOperation(value = "用户通知信息表-编辑")
    //@RequiresPermissions("message:SysTzMessage:edit")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody OaSysTzMessage oaSysTzMessage) {
        sysTzMessageService.updateById(oaSysTzMessage);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "用户通知信息表-通过id删除")
    @ApiOperation(value = "用户通知信息表-通过id删除")
    //@RequiresPermissions("message:SysTzMessage:delete")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        sysTzMessageService.removeById(id);
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "用户通知信息表-批量删除")
    @ApiOperation(value = "用户通知信息表-批量删除")
    //@RequiresPermissions("message:SysTzMessage:delete")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        sysTzMessageService.removeByIds(Arrays.asList(ids.split(",")));
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

//    /**
//     * 通过id查询
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "用户通知信息表-通过id查询")
//    @ApiOperation(value = "用户通知信息表-通过id查询")
//    @GetMapping(value = "/queryById")
//    public HttpResult<?> queryById(@RequestParam("id") String id) {
//        SysTzMessage sysTzMessage = sysTzMessageService.getById(id);
//        return HttpResult.ok(sysTzMessage);
//    }


    /**
     * 数据转换
     *
     * @param oaSysTzMessage
     */
    private void formatValue(OaSysTzMessage oaSysTzMessage) {
    }
}
